package com.geostart.constructionland.system.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author chengan
 * @description Redis工具类
 */
@Service
public class RedisServiceUtil {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 获取redis多个值
     * @param keys
     * @return
     */
    public  List<String> multiGet(Collection<String> keys) {
        return stringRedisTemplate.opsForValue().multiGet(keys);
    }

    /**
     * 获取存在某个前缀的结果
     * @param pattern
     * @return
     */
    public Set<String> getPatternKey(String pattern) {
        return stringRedisTemplate.keys(pattern);
    }

    /**
     * 是否有Key
     * @param key
     * @return
     */
    public boolean hasKey(String key) {
        try {
            return stringRedisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取单个Key的过期时间,单位为毫秒
     * @param key
     * @return
     */
    public Long getExpire(String key) {
        return stringRedisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
    }

    /**
     * 获取单个key的value
     * @param key
     * @return
     */
    public String get(String key) {
        return key == null ? null : stringRedisTemplate.opsForValue().get(key);
    }


    /**
     * 删除某个key的值
     * @param key
     * @return
     */
    public boolean delete(String key){
        return stringRedisTemplate.delete(key);
    }




}
